package org.jfinancegraph.gui;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.persistence.NoResultException;
import org.jfinanceobserver.persistence.ConnFactory;
import org.jfinanceobserver.persistence.jpa.controller.QuotesActiveJpaController;
import org.jfinanceobserver.persistence.jpa.controller.QuotesDataJpaController;
import org.jfinanceobserver.persistence.jpa.entities.QuotesActive;
import org.jfinanceobserver.persistence.jpa.entities.QuotesData;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

/**
 * This class build the TimePlot for a Quote
 *
 * $Author: r78v10a07@gmail.com $ $LastChangedDate: 2013-02-14 15:47:43 +0000 (Thu, 14 Feb 2013) $ $LastChangedRevision: 34 $
 *
 * @since Feb 13, 2013
 */
public class TimePlot {

    private List<String> symbol;
    private QuotesActiveJpaController aController;
    private QuotesDataJpaController dController;

    public TimePlot() {
        this.symbol = new ArrayList<>();
    }

    public TimeSeriesCollection getDataSet(String type, Date from, Date to) {
        QuotesActive quote;
        HashMap parm = new HashMap();
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        for (String symb : symbol) {
            TimeSeries pop = new TimeSeries(symb);
            try {
                parm.clear();
                parm.put("symbol", symb);
                quote = (QuotesActive) aController.useNamedQuerySingleResult("QuotesActive.findBySymbol", parm);

                parm.clear();
                parm.put("id", quote.getId());
                parm.put("from", from);
                parm.put("to", to);
                List<QuotesData> data = dController.useNamedQuery("QuotesData.findByActiveIdandLoadTime", parm);

                for (QuotesData d : data) {
                    switch (type) {
                        case "Ask":
                            pop.add(new Second(d.getLoadTime()), d.getAsk());
                            break;
                        case "Bit":
                            pop.add(new Second(d.getLoadTime()), d.getBid());
                            break;
                        case "Volume":
                            pop.add(new Second(d.getLoadTime()), d.getVolume());
                            break;
                    }

                }
                dataset.addSeries(pop);
            } catch (NoResultException ex) {
            }
        }
        return dataset;
    }

    public JFreeChart chartPlot(String type, Date from, Date to) {
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
                "",
                "Date",
                type,
                getDataSet(type, from, to),
                true,
                true,
                false);
        try {
            ChartUtilities.saveChartAsJPEG(new File("/home/roberto/chart.jpg"), chart, 500, 300);
        } catch (IOException e) {
            System.err.println("Problem occurred creating chart.");
        }

        XYPlot plot = chart.getXYPlot();
        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("HH:mm:ss"));

        return chart;
    }

    public ChartPanel timePlot(String type, Date from, Date to) {
        ChartPanel chartPanel = new ChartPanel(chartPlot(type, from, to));
        return chartPanel;
    }

    public List<String> getSymbol() {
        return symbol;
    }

    public void setaController(QuotesActiveJpaController aController) {
        this.aController = aController;
    }

    public void setdController(QuotesDataJpaController dController) {
        this.dController = dController;
    }
}
